package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.vo.ReservationRestCountVo;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
@Slf4j
public class CustomerReservationController extends BaseController {

    @Autowired
    private CustomerReservationService customerReservationService;

    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        Integer count = customerReservationService.getCancelledReservationCount();
        return success(count);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> limitSearch(
            @ApiParam(value = "页码") @RequestParam Integer pageNum,
            @ApiParam(value = "每页数量") @RequestParam Integer pageSize,
            @ApiParam(value = "状态") @RequestParam(required = false) Integer status
    ) {
        PageResponse<ReservationVo> pageResponse = customerReservationService.limitSearch(pageNum, pageSize, status);
        return success(pageResponse);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<ReservationRestCountVo>> getRestReservationCount(
            @ApiParam(value = "时间") @RequestParam Long time
    ) {
        List<ReservationRestCountVo> reservationRestCountVoList = customerReservationService.getRestReservationCount(time);
        return success(reservationRestCountVoList);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult insertReservation(@RequestBody ReservationDto reservationDto) {
        customerReservationService.insertReservation(reservationDto);
        return success();
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id) {
        customerReservationService.cancelReservation(id);
        return success();
    }

    @Scheduled(cron = "0 1,31 * * * ?")
    // @Scheduled(cron = "*/5 * * * * ?")
    public void updateExpiredReservations() {
        log.info("自动检测预约状态: " + LocalDateTime.now());
        customerReservationService.updateExpiredReservations();
    }

}